package com.vshop.platform.system.listener;

import com.vshop.auth.api.event.UpdateLoginInfoEvent;
import com.vshop.framework.mybatisflex.listener.EntityListenerManager;
import com.vshop.platform.system.crud.dao.SysUserDao;
import com.vshop.platform.system.domain.entity.SysUser;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@RequiredArgsConstructor
@Slf4j
@Component
public class UpdateLoginInfoEventListener {

    private final SysUserDao sysUserDao;

    /**
     * 更新登录信息
     */
    @Async
    @EventListener
    public void onUpdateLoginInfoEvent(UpdateLoginInfoEvent event) {
        SysUser sysUser = new SysUser();
        sysUser.setUserId(event.getUserId());
        sysUser.setLoginIp(event.getIp());
        sysUser.setLoginDate(LocalDateTime.now());

        // 不执行 更新数据 UpdateListener
        EntityListenerManager.withoutUpdateListener(() -> {
            sysUserDao.updateById(sysUser);
        });
    }
}
